class TimeLine {
  
  //slider bar and button
  float sliderBarX1, sliderBarY1;
  float sliderBarX2, sliderBarY2;
  float sliderBtn1X1, sliderBtn1Y1;
  float sliderBtn1X2, sliderBtn1Y2;
  float sliderBtn2X1, sliderBtn2Y1;
  float sliderBtn2X2, sliderBtn2Y2;
  int bWidth = 4; //button width
  //view zoom based on the slider
  int viewMin, viewMax;
  //for testing, rowCount is set here
  FloatTable eqData;
  int rowCount;
  float yearMin, yearMax; 
  int[] years; 
  boolean button = false;
  int count = 0;
  
  float startTime;
  float space;
  float playSpeed;
  boolean playTime;
  float b1Target;
  float b2Target;
  
  
  int tempSpeed = 10;
  

  
  
  TimeLine(int x1, int x2, int y1, int y2, int bWidth) {
    //corners of slider bar
    sliderBarX1 = x1;
    sliderBarX2 = x2;
    sliderBarY1 = y1;
    sliderBarY2 = y2;    
    
    this.bWidth = bWidth;
    sliderBtn1X1 = sliderBarX1;
    sliderBtn1X2 = sliderBarX1+bWidth;
    sliderBtn1Y1 = sliderBarY1 - 2;
    sliderBtn1Y2 = sliderBarY2 + 2;
    sliderBtn2X1 = sliderBarX2;
    sliderBtn2X2 = sliderBarX2+bWidth;
    sliderBtn2Y1 = sliderBtn1Y1;
    sliderBtn2Y2 = sliderBtn1Y2;
    
    //eqData = new FloatTable("earthquakes_compressed.csv");
    eqData = new FloatTable("timeLine.csv");
    rowCount = eqData.getRowCount();
    years = int(eqData.getRowNames());
    
    yearMin = getYearDecimal(0); 
    yearMax = getYearDecimal(years.length - 1); 
    
    viewMin = 0;
    viewMax = rowCount - 1;
    
    startTime = -1;
    space = (sliderBarX2 - sliderBarX1) / (yearMax - yearMin);
    //playSpeed = space / 10000;
   // playSpeed = space / 50000;
  // System.out.println("tempSpeed = "+tempSpeed);
   
    playSpeed = space / (5000 * tempSpeed);
    playTime = false;
    b1Target = 0;
    b2Target = 0;
  }
  
  
  void setTempSpeed(int temp){
  
    playSpeed = space / (5000 * temp);
  }
  
    //return month in decimal form
  float getMonthDecimal(int row) {
    return (eqData.getFloat(row, 0)-1)/12;
  }
  
  float getMonth(int row) {
    return eqData.getFloat(row, 0);
  }
  
  float getDay(int row) {
    return eqData.getFloat(row, 1);
  }
  
  //return year + month in decimal form
  float getYearDecimal(int row) {
    return years[row]+getMonthDecimal(row);
  }
  
  void draw() {
    /*println("drawing slidder");
    println(sliderBarX1 + " " + sliderBarX2);
    noStroke();
    fill(175, 100);
    rectMode(CORNERS);
    rect(mouseX, mouseY, mouseX+100, mouseY+100);
    rect(sliderBarX1, sliderBarX2, sliderBarY1,sliderBarY2);*/
    //Drawing ticks and years for slider bar
    fill(175);
    stroke(175);
    strokeWeight(2);
    textAlign(CENTER, TOP);
    textSize(9);
    float yearMinNum = years[0];
    float yearMaxNum = years[rowCount - 1];
    //text(years[0], sliderBarX1-5, sliderBarY2 + 8);
    //line(sliderBarX1, sliderBarY2, sliderBarX1, sliderBarY2+7);
    float yearLast = years[0];
    float monthLast = getMonth(0);
   // float dayLast = getDay(0);
    float yearsRange = yearMaxNum - yearMinNum;
    //float space = (sliderBarX2 - sliderBarX1) / (yearMax - yearMin);
    for (int y = 0; y <= yearsRange; y++) {
      float x = map(y+yearMinNum+1/12, yearMin, yearMax, sliderBarX1, sliderBarX2);
      line(x, sliderBarY2, x, sliderBarY2+7);
      if (y%2 == 0)
        text(int(y+yearMinNum), x, sliderBarY2 + 8);
    }
    
    //update position
    if (playTime) {
      float curTime = millis();
      float disToMove = playSpeed * (curTime - startTime);
      if (sliderBtn1X1 + disToMove > b1Target) {
        sliderBtn1X1 = b1Target;
        playTime = false;
      } else
        sliderBtn1X1 += disToMove;
      if (sliderBtn2X1 + disToMove > b2Target) {
        sliderBtn2X1 = b2Target;
        playTime = false;
      } else
        sliderBtn2X1 += disToMove;
      sliderBtn1X2 = sliderBtn1X1 + bWidth;
      sliderBtn2X2 = sliderBtn2X1 + bWidth;
    }
    
    strokeWeight(1);
    noFill();
    stroke(175, 50);
    rectMode(CORNERS);
    //draw slider bar rect
    rect(sliderBarX1, sliderBarY1, sliderBarX2, sliderBarY2);
    
    fill(255, 100);
    stroke(0);
    strokeWeight(1);
    //draw slider button 1
    rect(sliderBtn1X1-bWidth, sliderBtn1Y1, sliderBtn1X2, sliderBtn1Y2);
    
    fill(100, 100);
    //draw slider button 2
    rect(sliderBtn2X1-bWidth, sliderBtn2Y1, sliderBtn2X2, sliderBtn2Y2);
    
    //draw time highlight
    //color highlight = #81B4FA;
    color highlight = #81B4FA;
    fill(highlight,25);
    stroke(highlight, 100);
    rect(sliderBtn1X2, sliderBtn1Y1, sliderBtn2X1-bWidth, sliderBtn2Y2);
    
    this.viewMin = int(map(sliderBtn1X1, sliderBarX1, sliderBarX2, 0, rowCount-1));
    this.viewMax = int(map(sliderBtn2X1, sliderBarX1, sliderBarX2, 0, rowCount-1));
    
    noFill();
    rectMode(CORNER);
    textAlign(LEFT);
  }
  
  boolean mouseOver() {
    return (mouseX > sliderBtn1X1 - 10 && mouseX < sliderBtn2X2 + 10 && mouseY > sliderBtn1Y1 && mouseY < sliderBtn1Y2);
  }
  
  void play() {
    if (!playTime) {
     startTime = millis();
     sliderBtn2X1 = sliderBtn1X1 + space;
     sliderBtn2X2 = sliderBtn1X2 + space;
     b1Target = sliderBarX2-space;
     b2Target = sliderBarX2;
     playTime = true;
    }
  }
  
  void reset() {
    sliderBtn1X1 = sliderBarX1;
    sliderBtn1X2 = sliderBarX1+bWidth;
    sliderBtn2X1 = sliderBarX2;
    sliderBtn2X2 = sliderBarX2+bWidth;
  }
  
  void pause() {
    playTime = false;
  }
  
    
  float getYearMin() {
    return map(sliderBtn1X1, sliderBarX1, sliderBarX2, yearMin, yearMax);
  }
  
  float getMonthMin() {
    return ((getYearMin() - int(getYearMin())) * 12 + 1);
  }
  
  float getYearMax() {
    return map(sliderBtn2X1, sliderBarX1, sliderBarX2, yearMin, yearMax);
  }
  
  float getMonthMax() {
    return ((getYearMax() - int(getYearMax())) * 12 + 1);
  }
    
    
  
  void mouseDragged() {
    
    int dst = 5;
    if (mouseX >= sliderBtn1X1-dst && mouseX <= sliderBtn1X2+dst &&
          mouseY >= sliderBtn1Y1 && mouseY <= sliderBtn1Y2) {
           if (mouseX < sliderBarX1)
             sliderBtn1X1 = sliderBarX1; 
           else if (mouseX > sliderBtn2X1)
             sliderBtn1X1 = sliderBtn2X1 - 2 * bWidth;
           else
             sliderBtn1X1 = mouseX;
             
           sliderBtn1X2 = sliderBtn1X1 + bWidth;
           //drawSliderBar();
      } else if (mouseX >= sliderBtn2X1-dst && mouseX <= sliderBtn2X2+dst &&
          mouseY >= sliderBtn2Y1 && mouseY <= sliderBtn2Y2) {
           if (mouseX < sliderBtn1X2)
             sliderBtn2X1 = sliderBtn1X2; 
           else if (mouseX > sliderBarX2)
             sliderBtn2X1 = sliderBarX2;
           else
             sliderBtn2X1 = mouseX;
             
           sliderBtn2X2 = sliderBtn2X1 + bWidth;
           //drawSliderBar();
      } else if (mouseX >= sliderBtn1X1 && mouseX <= sliderBtn2X2 &&
          mouseY >= sliderBtn1Y1 && mouseY <= sliderBtn2Y2) {
           float deltaMouseX = mouseX - pmouseX;
           sliderBtn1X1 += deltaMouseX;
           sliderBtn2X1 += deltaMouseX;
           if (sliderBtn1X1 < sliderBarX1)
             sliderBtn1X1 = sliderBarX1; 
           else if (sliderBtn1X1 > sliderBtn2X1)
             sliderBtn1X1 = sliderBtn2X1 - 2 * bWidth;
           if (sliderBtn2X1 < sliderBtn1X2)
             sliderBtn2X1 = sliderBtn1X2; 
           else if (sliderBtn2X1 > sliderBarX2)
             sliderBtn2X1 = sliderBarX2;  
           
           sliderBtn1X2 = sliderBtn1X1 + bWidth;
           sliderBtn2X2 = sliderBtn2X1 + bWidth;
      }
  }
  
}
